//
//  SCShopMallController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/19.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCShopMallController.h"
#import "SDCycleScrollView.h"
#import "SCAllClassifyView.h"
#import "SCIntegralMallCell.h"
#import "SCIntegralMallSectionView.h"
#import "SCClassifyController.h"
#import "SCGoodsDetailController.h"
#import "SCSearchController.h"

@interface SCShopMallController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,SCIntegralMallCellDelegate,SCAllClassifyViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView      *headView;

@property (strong, nonatomic) SDCycleScrollView  *cycleScrollView;// 轮播图
@property (strong, nonatomic) SCModel            *model;
@property (strong, nonatomic) NSMutableArray     *sectionDataArray;
@property (strong, nonatomic) SCAllClassifyView  *classifyView;

@end

@implementation SCShopMallController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商城";
    self.lineView.hidden = NO;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerClass:[SCIntegralMallCell class] forCellReuseIdentifier:@"SCIntegralMallCellID"];
    
    //分类视图
    self.classifyView = [[SCAllClassifyView alloc]initWithUrl:@"admin.php?g=System&c=GoodsApp&a=category_list"];
    self.classifyView.delegate = self;
   
    //设置轮播图
    [self setUpScrollView];
    
    //请求数据
    [self requestData];
}
- (void)setUpScrollView{
    
    self.headView.height = kScreenW*80/207;
    
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenW, kScreenW*80/207) delegate:self placeholderImage:[UIImage imageWithColor:BGColor]];
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.currentPageDotColor = RGB(251, 30, 72);
    self.cycleScrollView.pageDotColor = [UIColor whiteColor];
    self.cycleScrollView.autoScrollTimeInterval = 3.0f;
    self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    [self.headView addSubview:self.cycleScrollView];
}
#pragma mark - tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionDataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCIntegralMallCell *cell = (SCIntegralMallCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return [cell geCellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SCModel *model = self.sectionDataArray[section];
    if (model.list.count == 0) {
        return 0.01;
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SCModel *model = self.sectionDataArray[section];
    if (model.list.count == 0) {
        return nil;
    }
    SCIntegralMallSectionView *sectionView = [SCIntegralMallSectionView addIntegralMallSectionView];
    sectionView.titleLab.text = model.cat_name;
    sectionView.moreBtn.tag = section;
    [sectionView.moreBtn addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
    return sectionView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCIntegralMallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCIntegralMallCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SCModel *model = self.sectionDataArray[indexPath.section];
    cell.listArray = model.list;
    cell.delegate = self;
    return cell;
}
- (void)clickIntegralMallGoodsModel:(SCModel *)model{
    
    SCGoodsDetailController *vc = [[SCGoodsDetailController alloc]init];
    vc.goodsId = model.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 广告轮播图
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}
#pragma mark - 点击按钮点击事件

- (IBAction)clickSearchBtn:(UIButton *)sender {
    
    [self.classifyView dismiss];
    
    SCSearchController *vc = [[SCSearchController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)clickAllClassifyBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [self.classifyView show];
    }else{
        [self.classifyView dismiss];
    }
}

//更多
- (void)clickMore:(UIButton *)sender{
    
    SCModel *model = self.sectionDataArray[sender.tag];
    SCClassifyController *vc = [[SCClassifyController alloc]init];
    if (sender.tag == 0) {
        vc.type = @"1";
        vc.cat_fid = @"";
        vc.title = @"商品推荐";
    }else{
        vc.type = @"2";
        vc.cat_fid = model.cat_id;
        vc.title = model.cat_name;
    }
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)clickClassifyWithModel:(SCModel *)model{
    
    SCClassifyController *vc = [[SCClassifyController alloc]init];
    vc.type = @"2";
    vc.cat_fid = model.cat_id;
    vc.title = model.cat_name;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 请求数据
- (void)requestData{
    
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GoodsApp&a=index"];
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] GET:url parameters:nil finished:^(id result, NSString *error) {
        NSLog(@"返回的数据:%@",result);
        [SVProgressHUD dismiss];
        
        self.model = [SCModel mj_objectWithKeyValues:result[@"data"]];
        self.model.lun = [SCModel mj_objectArrayWithKeyValuesArray:self.model.lun];
        self.model.recommend = [SCModel mj_objectArrayWithKeyValuesArray:self.model.recommend];
        self.model.cat = [SCModel mj_objectArrayWithKeyValuesArray:self.model.cat];
        
        for (SCModel *model in self.model.cat) {
            model.list = [SCModel mj_objectArrayWithKeyValuesArray:model.list];
        }
        self.sectionDataArray = [NSMutableArray array];
        if (self.model.recommend.count != 0) {
            SCModel *model = [[SCModel alloc]init];
            model.cat_id = @"0";
            model.cat_name = @"为您推荐";
            model.list = [self.model.recommend copy];
            [self.sectionDataArray addObject:model];
        }
        [self.sectionDataArray addObjectsFromArray:self.model.cat];
        
        NSMutableArray *scrollImages = [NSMutableArray array];
        for (SCModel *lunModel in self.model.lun) {
            [scrollImages addObject:[NSString stringWithFormat:@"%@%@",Lun_ImageURL,lunModel.bg_pic]];
        }
        self.cycleScrollView.imageURLStringsGroup = [scrollImages copy];
        [self.tableView reloadData];
    }];
}

@end
